package com.kgc.kmall.kmallmanagerservice.service;

import com.alibaba.fastjson.JSON;
import com.kgc.kmall.bean.*;
import com.kgc.kmall.kmallmanagerservice.mapper.PmsSkuAttrValueMapper;
import com.kgc.kmall.kmallmanagerservice.mapper.PmsSkuImageMapper;
import com.kgc.kmall.kmallmanagerservice.mapper.PmsSkuInfoMapper;
import com.kgc.kmall.kmallmanagerservice.mapper.PmsSkuSaleAttrValueMapper;
import com.kgc.kmall.service.SkuService;
import com.kgc.kmall.util.RedisUtil;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RedissonClient;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;


import java.util.concurrent.locks.Lock;

@Service
public class SkuServiceImpl implements SkuService {
    @Resource
    PmsSkuInfoMapper pmsSkuInfoMapper;

    @Resource
    PmsSkuAttrValueMapper pmsSkuAttrValueMapper;

    @Resource
    PmsSkuSaleAttrValueMapper pmsSkuSaleAttrValueMapper;

    @Resource
    PmsSkuImageMapper pmsSkuImageMapper;
    @Resource
    RedisUtil redisUtil;


    @Resource
    RedissonClient redissonClient;

    @Override
    public int saveSkuInfo(PmsSkuInfo pmsSkuInfo) {
        int i = pmsSkuInfoMapper.insert(pmsSkuInfo);
    //获取skuid
        Long pmsSkuInfoId = pmsSkuInfo.getId();
        List<PmsSkuAttrValue> skuAttrValueList = pmsSkuInfo.getSkuAttrValueList();
        if(skuAttrValueList!=null&&skuAttrValueList.size()>0){
            pmsSkuAttrValueMapper.addPmsSkuAttrValue(pmsSkuInfoId,skuAttrValueList);
        }

        List<PmsSkuSaleAttrValue> skuSaleAttrValueList = pmsSkuInfo.getSkuSaleAttrValueList();
        if(skuSaleAttrValueList!=null&&skuSaleAttrValueList.size()>0){
            pmsSkuSaleAttrValueMapper.addPmsSkuSaleAttrValue(pmsSkuInfoId,skuSaleAttrValueList);
        }

        List<PmsSkuImage> skuImageList = pmsSkuInfo.getSkuImageList();
        if(skuImageList!=null&&skuImageList.size()>0){
            pmsSkuImageMapper.addPmsSkuImage(pmsSkuInfoId,skuImageList);
        }
        return i;
    }

    @Override
    public PmsSkuInfo selectBySkuId(Long skuId) {
       /* PmsSkuInfo pmsSkuInfo =null;
        String key="sku:"+skuId+":info";
        Jedis jedis=redisUtil.getJedis();
        String skuinfojson = jedis.get(key);
        if(skuinfojson!=null){
        //缓存里面有
            if(skuinfojson.equals("empty")){
                return new PmsSkuInfo();
            }
             pmsSkuInfo = JSON.parseObject(skuinfojson, PmsSkuInfo.class);
        }else{
            String skuKey="sku:"+skuId+":lock";
            String skuLockValue = UUID.randomUUID().toString();
            String lock = jedis.set(skuKey, skuLockValue, "NX", "PX",600*1000);
            System.out.println("lock:"+lock);
            if(lock!=null&&lock.equals("OK")){
                //缓存里面没有
                pmsSkuInfo = pmsSkuInfoMapper.selectByPrimaryKey(skuId);
                //判断是否查到数据,结局缓存穿透问题
                if(pmsSkuInfo!=null){
                    String toJSON = JSON.toJSONString(pmsSkuInfo);
                    //这只有效期的不同,解决缓存雪崩问题
                    Random random=new Random();
                    int i = random.nextInt(10);
                    jedis.setex(key,i*60*1000,toJSON);
                }else{

                    jedis.setex(key,5*60*1000,"empty");
//                    return new PmsSkuInfo();
                }

                String skuLockValue2  = jedis.get(skuKey);
                System.out.println("skuLockValue2:"+skuLockValue2);
                System.out.println("skuLockValue"+skuLockValue);
                if(skuLockValue2!=null&&skuLockValue2.equals(skuLockValue)){
                    jedis.del(skuKey);
                }

//                String script ="if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
//                jedis.eval(script, Collections.singletonList(skuKey),Collections.singletonList(skuLockValue));


            }else{
                try {
                    Thread.sleep(3*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return selectBySkuId(skuId);
            }



        }
//        jedis.close();
        return pmsSkuInfo;*/



        PmsSkuInfo pmsSkuInfo =null;
        String key="sku:"+skuId+":info";
        Jedis jedis=redisUtil.getJedis();
        String skuinfojson = jedis.get(key);
        if(skuinfojson!=null){
            //缓存里面有
            if(skuinfojson.equals("empty")){
                return new PmsSkuInfo();
            }
            pmsSkuInfo = JSON.parseObject(skuinfojson, PmsSkuInfo.class);
        }else{
            Lock lock =redissonClient.getLock("lock");
            lock.lock();//上锁
                //缓存里面没有
                pmsSkuInfo = pmsSkuInfoMapper.selectByPrimaryKey(skuId);
                //判断是否查到数据,结局缓存穿透问题
                if(pmsSkuInfo!=null){
                    String toJSON = JSON.toJSONString(pmsSkuInfo);
                    //这只有效期的不同,解决缓存雪崩问题
                    Random random=new Random();
                    int i = random.nextInt(10);
                    jedis.setex(key,i*60*1000,toJSON);
                }else{

                    jedis.setex(key,5*60*1000,"empty");
//                    return new PmsSkuInfo();
                }
            lock.unlock();// 解锁
            }

        return pmsSkuInfo;
    }




    @Override
    public List<PmsSkuInfo> seleByViews(Long spuId) {
        Jedis jedis=redisUtil.getJedis();
        String skuKey="spu:"+spuId+":info";
        String skuInfoJson = jedis.get(skuKey);
        List<PmsSkuInfo> pmsSkuInfos =null;
        if(skuInfoJson!=null){
        //说明缓存里面有
            if(skuInfoJson.equals("empty")){
                //此为空值
                return new ArrayList<>();
            }
            pmsSkuInfos = JSON.parseArray(skuInfoJson, PmsSkuInfo.class);
        }else{
        //说明缓存里面没有
            Lock lock=redissonClient.getLock("spuIdLock");
            lock.lock();
            Random random=new Random();
            int i = random.nextInt(10);
            pmsSkuInfos = pmsSkuInfoMapper.selePmsInfoByViews(spuId);
            if(pmsSkuInfos.size()<=0){
                jedis.setex(skuKey,(i+1)*60*1000,"empty");
            }else{
            jedis.setex(skuKey,i*60*1000,JSON.toJSONString(pmsSkuInfos));

            lock.unlock();
            }
        }

        return pmsSkuInfos;
    }

    @Override
    public List<PmsSkuInfo> getAllSku() {
        //List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoMapper.selectByExample(null);
        List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoMapper.selectByExample(null);
        for (PmsSkuInfo pmsSkuInfo : pmsSkuInfos) {
            System.out.println(pmsSkuInfo.toString());
        }
        for (PmsSkuInfo pmsSkuInfo : pmsSkuInfos) {
            PmsSkuAttrValueExample example=new PmsSkuAttrValueExample();
            PmsSkuAttrValueExample.Criteria criteria = example.createCriteria();
            criteria.andSkuIdEqualTo(pmsSkuInfo.getId());
            List<PmsSkuAttrValue> pmsSkuAttrValues = pmsSkuAttrValueMapper.selectByExample(example);
            pmsSkuInfo.setSkuAttrValueList(pmsSkuAttrValues);
        }
        return pmsSkuInfos;
    }

    @Override
    public boolean checkPrice(Long productSkuId, BigDecimal price) {
        boolean b=false;
        PmsSkuInfo pmsSkuInfo = pmsSkuInfoMapper.selectByPrimaryKey(productSkuId);
        BigDecimal bigDecimal=new BigDecimal(pmsSkuInfo.getPrice());
        if(price.compareTo(bigDecimal)==0){
            b=true;
        }
        return b;
    }
}
